﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using HZ.MBSM.BLL;
using Grove.ORM;
using HZ.MBSM.Model;
using HZ.MBSM.DBUtility;
using System.Drawing;
public partial class Bidder_GoodsList : System.Web.UI.Page,IPageDescribe
{
    protected void Page_Load(object sender, EventArgs e)
    {

        if (HZ.MBSM.BLL.Login.CheckAssert(this.UseAccountType))
        {
            HZ.MBSM.Model.Account _Account = (HZ.MBSM.Model.Account)Session["Account"];
            if (_Account != null)
            {               
                if (!IsPostBack)
                {
                    lstMemo3.Items.Add("全部");
                    lstMemo3.Items.Add("未保存");
                    lstMemo3.Items.Add("已保存未提交");
                    lstMemo3.Items.Add("已提交");
                    lstMemo3.Items.Add("已修改");
                    GetProject();
                    Button2show();//如果无审核通过的商品，“投标人维护”不显示  
                }
                
                   
              
                if (Request.Form["SubDel"] == "删除商品")
                {
                    String _strdel = Request.Form["txtDel"];
                    Db db = new Db();
                    String Sql = "delete from goods Where Goodsid in (" + _strdel + ") and Checkstatus=0 and account_tjr=" + _Account.AccountId.ToString();
                    db.Command(Sql);
                    db.Close();
                    // HZ.MBSM.Model.Account _Account = (HZ.MBSM.Model.Account)Session["Account"];
                    HZ.MBSM.BLL.LogManage.RecordLoginDetail(_Account.AccountName, Request.UserHostAddress, "删除", "goods", Sql, "../Bidder/GoodsList.aspx");

                }

                if (Request.Form["SubRecover"] == "恢复商品")
                {
                    String _strRecover = Request.Form["txtDel"];
                    Db db = new Db();
                    String Sql = "Update Goods SET IsDel='1' Where Goodsid in (" + _strRecover + ")";
                    db.Command(Sql);
                    db.Close();
                    HZ.MBSM.BLL.LogManage.RecordLoginDetail(_Account.AccountName, Request.UserHostAddress, "更新", "goods", Sql, "../Bidder/GoodsList.aspx");

                }
                string sort = "";
                sort = _Account.Sort;
                if (sort.Equals("0"))
                {                   
                    showGridView();
                }
                else
                {
                    Response.Write("<script>window.alert('此功能对经营企业不适用！');</script>");
                    Response.Write("<script>window.location.href='ProjectList.aspx'</script>");

                }
            }

        }
    }

    //得到项目号
    private void GetProject()
    {
      string  strSql = "select projectid,ProjectMarker,Title from project order by projectid desc";
      Db db = new Db();
      System.Data.IDataReader dr = db.DataReader(strSql);
        listProject.DataSource = dr;
        listProject.DataTextField = "Title";
        listProject.DataValueField = "projectid";
        listProject.DataBind();
        dr.Close();
        dr.Dispose();
        db.Close();
    
    }
    public void showGridView()
    {

        string strConditions = fctConditions();
        Db db = new Db();
        DbPage dbPage = new DbPage(db);
        dbPage.Table = "product inner join goods on product.productid=goods.productid inner join company on goods.Accountid_sc=company.accountid left join bid_goods_tbr on goods.goodsid=bid_goods_tbr.goodsid";                                               //要查询的表明
        dbPage.Key = "goods.goodsid";                                               //表的主键    
        dbPage.Where = strConditions;                                           //查询条件    
        dbPage.SelectField = "goods.GoodsId,Product.Productid,ProductName,Outlookc,shenhetime,Medicinemodel,g_unit,xishu,factor,case goods.CheckStatus when '0' then '未审核' when '1' then '审核通过' when '2' then '审核未通过' end as CheckStatus,nopass,case  when goods.Memo3='0' then '已保存未提交' when goods.Memo3 is null then '未保存'  when goods.Memo3='1' then '已提交'  when goods.Memo3='2' then '已修改'  else '未知' end memo3,CASE Goods.QualityLevels " + ConfigurationManager.AppSettings["QualityLevels"].ToString() + " END AS QualityLevels,case ifbid when '0' then '否' else  '是' end as ifbid,bid_goods_tbr.status ";//要显示的字段
        dbPage.PageIndex = SysFun.ToInt(Request["CurrentlyPageIndex"]);          //要显示第几页   
        dbPage.PageSize = 10;                                                    //没有显示记录条数   
        dbPage.OrderBy = "productcode,productname,medicinemodel,outlookc,goods.GoodsId";                  //排序字段
        System.Data.IDataReader dr = dbPage.ExecuteReader();
        GridView1.DataSource = dr;
        GridView1.DataBind();
        dr.Close(); 
        //dr.Dispose();
        dbPage.Db.Close();
        db.Close();
        //08.18
        dr = null;
        db = null;

        this.DbPageFlip1.DbPage = dbPage;//将数据传给控键

    }

    public string fctConditions() 
    {
        Int64 Account_SC;
        HZ.MBSM.Model.Account _Account = (HZ.MBSM.Model.Account)Session["Account"];

        string _strWhere = "";
        if (_Account != null)
        {
            Account_SC = _Account.AccountId;

            _strWhere = @"  goods.Accountid_sc=" + Account_SC;

            //药品通用名或拼音码

            string _strProductName = SysFun.ToTrim(txtProductName.Text);
            if (_strProductName != "")
            {
                _strWhere += " and (ProductName like '%" + _strProductName + "%' or spellingcode like '%" + _strProductName + "%') ";
            }
            //企业是否确认投标
            string _ifbid = DropDownList1.Text.ToString(); ;
            switch (_ifbid)
            {
                case "全部":
                    break;
                case "是":
                    _strWhere += "  and isnull(ifbid,1)=1 ";
                    break;
                case "否":
                    _strWhere += " and ifbid=0 ";
                    break;
            }

            //规格
            string _strOutlookc = SysFun.ToTrim(txtOutlookc.Text);
            if (_strOutlookc != "")
            {
                _strWhere += " and Outlookc like '%" + _strOutlookc + "%'";
            }
            //品目号 采用productid作为品目号显示在商品列表中 edit by ws 2008.9.26

            string _strProductCode = SysFun.ToTrim(txtProductCode.Text);
            if (_strProductCode != "")
            {
                _strWhere += " and Product.Productid like '%" + _strProductCode + "%'";
            }

            //流水号

            string _strGoodsId = SysFun.ToTrim(txtGoodsId.Text);
            if (_strGoodsId != "")
            {
                _strWhere += " And goods.Goodsid =" + _strGoodsId + " ";
            }

            //提交状态

            string _strMemo3 = lstMemo3.SelectedValue;
            switch (_strMemo3)
            {
                case "全部":
                    break;
                case "未保存":
                    _strWhere += " and goods.Memo3 is null  ";
                    break;
                case "已保存未提交":
                    _strWhere += " and goods.Memo3=0  ";
                    break;
                case "已提交":
                    _strWhere += " and goods.Memo3=1 ";
                    break;
                case "已修改":
                    _strWhere += " and goods.Memo3=2 ";
                    break;
            }
           
            string _strCheckStatus = DDLCheckStatus.SelectedValue.ToString();
            switch (_strCheckStatus)
            {
                case "全部":
                    
                    break;
                case "未审核":
                    _strWhere += " And goods.checkstatus=0";
                    break;
                case "审核通过":
                    _strWhere += " And goods.checkstatus=1";
                    break;
                case "审核未通过":
                    _strWhere += " And goods.checkstatus=2";
                    break;
            }
        }
        return _strWhere;
    }

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
       
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            if (e.Row.Cells[9].Text == "审核未通过")//审核状态

            {
                e.Row.BackColor = Color.Yellow;
            }

            if (e.Row.Cells[14].Text == "1")//审核状态
            {
                e.Row.Cells[13].Text = "";
            }
            e.Row.Cells[14].Visible = false;
        }
       


    }
    
    public void Button2show()
    {
        HZ.MBSM.Model.Account _Account = (HZ.MBSM.Model.Account)Session["Account"];
        string str_sql = "select count(1) from v_product_goods a left join bid_project_product b on a.productid=b.productid where a.checkstatus=1 and a.companycheck=1 and b.projectid=" + listProject.SelectedItem.Value + " and Accountid_sc=" + _Account.AccountId;
        Db db = new Db();
        DataTable gg = db.DataTable(str_sql);
        if (gg != null && gg.Rows[0][0].Equals(0) == false)
        {
            Button2.Visible = true;
        }
        db.Close();
        gg.Dispose();   
        
        //这里你只限定了goods.checkstatus =1 但审查通过的商品里也有可能是自行采购的,此时就无需再维护投标人,只有招标目录的商品审核通过才显示
        //而且生产企业本身未通过审核则也无维护投标人的必要
        //v_product_goods视图已经有你写的这个连接查询可以直接使用
        //用count(1)查询总行数的效率比count(*)快的多;
        //string str_sql = " select count(*) from  product inner join goods on product.productid=goods.productid inner join company on goods.Accountid_sc=company.accountid where goods.checkstatus =1 and Accountid_sc=" + _Account.AccountId;
        // int _check ;
        //Db db = new Db();
        //DataTable gg = db.DataTable(str_sql);
        //_check =Convert.ToInt32(gg.Rows[0][0].ToString().Trim());
        //if (_check == 0)
        //{
        //    Button2.Visible = false;//默认button为false 只有当查询到值的时候才执行,所以无需执行Button2.Visible = false;
        //}
        //else
        //{ Button2.Visible = true; }
        //db.Close();
        //gg.Dispose();      
    
    

    }

    #region IPageDescribe 成员

  
    public string Describe
    {
        get { return ""; }
    }
    public string Name
    {
        get { return "本企业商品信息"; }
    }
    public string ParentName
    {
        get { return "商品维护"; }
    }

    public System.Collections.Generic.List<HZ.MBSM.DAL.AccountType> UseAccountType
    {
        get
        {
            System.Collections.Generic.List<HZ.MBSM.DAL.AccountType> _AccountType = new System.Collections.Generic.List<HZ.MBSM.DAL.AccountType>();
            _AccountType.Add(HZ.MBSM.DAL.AccountType.enumCompany);
            return _AccountType;
        }
    }

    #endregion




  

}
